package com.igeek.collateral.service.Impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.User;
import com.igeek.collateral.entity.UserRole;
import com.igeek.collateral.mapper.RoleMenuMapper;
import com.igeek.collateral.mapper.UserMapper;
import com.igeek.collateral.mapper.UserRoleMapper;
import com.igeek.collateral.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.utils.QiniuUtils;
import com.igeek.collateral.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional(readOnly = true)
    @Override
    public Page<User> findPage(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<User> page = new Page<>(currentPage , pageSize);
        //封装查询条件
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasLength(queryString) , User::getUsername , queryString);

        //分页查询数据
        Page<User> userPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return userPage;
    }

    @Override
    public boolean add(User user, Integer[] roleIds) {
        int i = baseMapper.insert(user);
        addUserRole(user,roleIds);
        return i>0?true:false;

    }


    @Transactional
    @Override
    public boolean updateUser(User user, Integer[] roleIds){
        userRoleMapper.delete(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId,user.getId()));
        this.addUserRole(user,roleIds);
        return this.baseMapper.updateById(user)>0?true:false;
    }

    @Override
    public List<Integer> findRoleByUserId(Integer userId) {

        List<UserRole> list=userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId,userId));
        List<Integer> roleIds=list.stream().map(UserRole::getRoleId).collect(Collectors.toList());
        return roleIds;
    }

    //七牛云上传
    public String uploadByQiniuyun(MultipartFile imgFile) throws IOException {
        if(imgFile!=null){
            String oldName = imgFile.getOriginalFilename();
            if(StringUtils.hasLength(oldName)){
                String newName = UUID.randomUUID()+oldName;
                //上传至七牛云服务器
                InputStream inputStream =imgFile.getInputStream();
                String pic = QiniuUtils.uploadForQiniu(inputStream, newName);
                return pic;
            }
        }
        return null;
    }

    @Override
    public boolean del(Integer userId) {
        userRoleMapper.delete(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId));
        int d= this.baseMapper.deleteById(userId);
        return d>0?true:false;
    }

    private void addUserRole(User user, Integer[] roleIds){
        Stream.of(roleIds).forEach(roleId->{
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(roleId);
            userRoleMapper.insert(userRole);
        });
    }
}
